package com.example.project.webControlleer;

import com.example.project.dao.CarDAO;
import com.example.project.dao.ParkingDAO;
import com.example.project.dao.UserDAO;
import com.example.project.dao.WalletHistroyDAO;
import com.example.project.pojo.Car;
import com.example.project.pojo.Parking;
import com.example.project.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("web")
public class WebLeaseParkingController {
   private final ParkingDAO parkingDAO =new ParkingDAO();

    @RequestMapping("leaseParking")
    public String leaseParking(Model model, HttpSession session){


        List<Parking>parkingList=parkingDAO.FindAllBeforeLease();
        User user =(User) session.getAttribute("user");
        CarDAO carDAO =new CarDAO();
        UserDAO userDAO=new UserDAO();
        double money= userDAO.findTotalMoneyByID(user.getId());
        model.addAttribute("money",money);
        List<Car> carList= carDAO.findCarByuserID(user.getId());
        if (!model.containsAttribute("parkingList2")){
            model.addAttribute("parkingList",parkingList);
        }

        model.addAttribute("carList",carList);
        return "/web/leaseParking";
    }
    @RequestMapping("checkBeforeLease")
    public String checkBeforeLease(String condition,String value,Model model,HttpSession session){
        List<Parking>parkingList =new ArrayList<>();
        switch (condition){
            case "id":
                Integer id =Integer.parseInt(value);
              parkingList = parkingDAO.checkBeforeLeaseById(id);
                break;
            case "region":
                parkingList =parkingDAO.checkBeforeLeaseByRegion(value);
                break;
            case "beizhu":
                parkingList =parkingDAO.checkBeforeLeaseByBeiZhu(value);
                break;
            case "parkingtype":
                parkingList =parkingDAO.checkBeforeLeaseByType(value);
                break;
        }
        User user =(User) session.getAttribute("user");
        UserDAO userDAO=new UserDAO();
        double money= userDAO.findTotalMoneyByID(user.getId());
        model.addAttribute("money",money);
        CarDAO carDAO =new CarDAO();
       model.addAttribute("parkingList",parkingList);
        List<Car> carList= carDAO.findCarByuserID(user.getId());
        model.addAttribute("carList",carList);
           return  "/web/leaseParking";
    }
    @RequestMapping("lease")
    @ResponseBody
    public String lease(Parking parking,Integer year,double money,HttpSession session,Integer car_id){
        User user =(User) session.getAttribute("user");
        parking.yearTime(year);
        CarDAO carDAO =new CarDAO();
        Car car =carDAO.findCarByID(car_id);
        car.setUser(user);
        parking.setCar(car);
        UserDAO userDAO =new UserDAO();
        user.setTotalMoney(userDAO.findTotalMoneyByID(user.getId()));
        money = money*year;
       userDAO.charge(-money,user);
            parkingDAO.lease(parking);
            session.setAttribute("user",user);
        WalletHistroyDAO walletHistroyDAO =new WalletHistroyDAO();
        walletHistroyDAO.parkingHistory(parking,user,money);
            return "";


    }
}
